﻿using B.S.BaseData.Api.Write.Applicantion.Command.GoodsCommand;
using B.S.BaseData.Api.Write.GetCelim;
using B.S.BaseData.ErrorCode;
using B.S.BaseData.Infrastructure.Interface;
using B.S.BataBase.Domain.Entitys;
using MediatR;
using Yitter.IdGenerator;

namespace B.S.BaseData.Api.Write.Applicantion.CommandHandler.GoodsCommandHandler
{
    public class GoodscultivationAddCommandHandler : IRequestHandler<GoodscultivationAddCommand, APIResult<int>>
    {
        private readonly IBaseRepository<GoodscultivationInfo> _cultivationrepository;
        private readonly IIdContextCemil _iidContextCemil;
        public GoodscultivationAddCommandHandler(IBaseRepository<GoodscultivationInfo> cultivationrepository, IIdContextCemil iidContextCemil)
        {
            _cultivationrepository = cultivationrepository;
            _iidContextCemil = iidContextCemil;
        }
        public async Task<APIResult<int>> Handle(GoodscultivationAddCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result=new APIResult<int>();
            try
            {
                GoodscultivationInfo info=new GoodscultivationInfo();
                info.GoodscultivationHead=request.GoodscultivationHead;
                info.GoodscultivationDay=request.GoodscultivationDay;
                info.GoodscultivationName=request.GoodscultivationName;
                info.Goodscultivationbrand= request.Goodscultivationbrand;
                info.GoodscultivationDate=request.GoodscultivationDate;
                info.Goodscultivationsource=request.Goodscultivationsource;
                info.Id=YitIdHelper.NextId();
                info.IsDeleted = false;
                info.CreateName= _iidContextCemil.UserName;
                info.GoodsId=request.GoodsId;
                result.Code=ApiEnum.Success;
                result.Msg = "养殖信息添加成功";
                result.Data=await _cultivationrepository.CreateAsync(info);
                return await Task.FromResult(result);
            }
            catch (Exception)
            {
                result.Code = ApiEnum.Fail;
                result.Msg = "养殖信息添加失败";
                return await Task.FromResult(result);
                throw;
            }
        }
    }
}
